<template>
  <div class="student">
    <h2>学生姓名: {{ name }}</h2>
    <h2>学生性别: {{ gender }}</h2>
    <button @click="sendStudentMessage">点我传递学生信息</button>
    <p>要入学的学校信息: {{ schoolMessage }}</p>
  </div>
</template>

<script>
export default {
  name: "student-name",
  data() {
    return {
      name: "张三",
      gender: "男",
      schoolMessage: "",
    };
  },
  methods: {
    sendStudentMessage() {
      console.log("查看组件实例vc:", this);
      this.$bus.$emit("getStudent-message", this.name, this.gender);
    },
  },
  mounted() {
    this.$bus.$on("getSchool-message", (...val) => {
      this.schoolMessage = val;
    });
  },
  beforeDestroy() {
    this.$bus.$off("getSchool-message");
  },
};
</script>

<style lang="less" scoped>
.student {
  background-color: skyblue;
  padding: 10px;
  margin: 10px;
}
</style>
